<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="教室编号" prop="roomCode">
        <el-input
          v-model="queryParams.roomCode"
          placeholder="请输入教室编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教室类型" prop="classroomType">
        <el-select v-model="queryParams.classroomType" placeholder="请选择教室类型" clearable style="width: 150px;">
          <el-option
            v-for="dict in classroom_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- 搜索表单中的所属学院 -->
      <el-form-item label="所属学院" prop="collegeId">
        <el-select
          v-model="queryParams.collegeId"
          placeholder="请输入学院名称/代码"
          clearable
          filterable
          remote
          :remote-method="filterColleges"
          @change="handleQuery"
          style="width: 200px;"
        >
          <el-option
            v-for="college in filteredColleges"
            :key="college.collegeId"
            :label="`${college.collegeName} (${college.collegeCode})`"
            :value="college.collegeId"
          >
            <span style="float: left">{{ college.collegeName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ college.collegeCode }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="校区" prop="campus">
        <el-select v-model="queryParams.campus" placeholder="请选择校区" clearable style="width: 150px;">
          <el-option
            v-for="dict in campus_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教学楼" prop="buildingId">
        <el-input
          v-model="queryParams.buildingId"
          placeholder="请输入教学楼"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomNumber">
        <el-input
          v-model="queryParams.roomNumber"
          placeholder="请输入具体房间号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上课人数" prop="capacity">
        <el-input
          v-model="queryParams.capacity"
          placeholder="请输入上课人数容量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试人数" prop="testCapacity">
        <el-input
          v-model="queryParams.testCapacity"
          placeholder="请输入考试人数容量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有黑板" prop="blackboard">
        <el-select v-model="queryParams.blackboard" placeholder="请选择是否有黑板" clearable style="width: 150px;">
          <el-option
            v-for="dict in y_n"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有投影仪" prop="projector">
        <el-select v-model="queryParams.projector" placeholder="请选择是否有投影仪" clearable style="width: 150px;">
          <el-option
            v-for="dict in y_n"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有白板" prop="whiteboard">
        <el-select v-model="queryParams.whiteboard" placeholder="请选择是否有白板" clearable style="width: 150px;">
          <el-option
            v-for="dict in y_n"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入联系人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable style="width: 150px;">
          <el-option
            v-for="dict in status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排课是否可用" prop="scheduleAvailable">
        <el-select v-model="queryParams.scheduleAvailable" placeholder="请选择排课是否可用" clearable style="width: 150px;">
          <el-option
            v-for="dict in y_n"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="考试是否可用" prop="testAvailability">
        <el-select v-model="queryParams.testAvailability" placeholder="请选择考试是否可用" clearable style="width: 150px;">
          <el-option
            v-for="dict in y_n"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="限制时间" prop="bindingHours">
        <el-date-picker clearable
          v-model="queryParams.bindingHours"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择限制时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="单双周" prop="oddBiweekly">
        <el-input
          v-model="queryParams.oddBiweekly"
          placeholder="请输入单双周"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['classroom:classroom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['classroom:classroom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['classroom:classroom:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['classroom:classroom:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport">导入</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classroomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="教室名称" align="center" prop="classRoomName" />
      <el-table-column label="教室编号" align="center" prop="roomCode" />
      <el-table-column label="教室类型" align="center" prop="classroomType">
        <template #default="scope">
          <dict-tag :options="classroom_type" :value="scope.row.classroomType"/>
        </template>
      </el-table-column>
      <el-table-column label="教学功能区" align="center" prop="teachingFunctionArea" />
      <el-table-column label="所属学院" align="center" prop="collegeId" />
      <el-table-column label="校区" align="center" prop="campus">
        <template #default="scope">
          <dict-tag :options="campus_type" :value="scope.row.campus"/>
        </template>
      </el-table-column>
      <el-table-column label="教学楼id" align="center" prop="buildingId" />
      <el-table-column label="有效座位数" align="center" prop="effectiveSeats" />
      <el-table-column label="座位数" align="center" prop="seating" />
      <el-table-column label="考试座位数" align="center" prop="examSeating" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['classroom:classroom:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['classroom:classroom:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 导入对话框 -->
    <el-dialog :title="upload.titleImport" v-model="upload.openImport" width="400px" append-to-body>
      <el-upload
          ref="uploadRef"
          :limit="1"
          accept=".xlsx, .xls"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :on-error="handleFileError"
          :auto-upload="false"
          drag
          @change="handleFileChange"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center">
          <div>
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="handleCancelImport">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 添加或修改教学资源管理对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body >
      <el-form ref="classroomRef" :model="form" :rules="rules" label-width="100px" class="two-column-form">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="教室编号" prop="roomCode">
              <el-input v-model="form.roomCode" placeholder="请输入教室编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="教室类型" prop="classroomType">
              <el-select v-model="form.classroomType" placeholder="请选择教室类型">
                <el-option
                  v-for="dict in classroom_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 添加/修改表单中的所属学院 -->
         <!-- 所属学院与校区 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属学院" prop="collegeId">
              <el-select
                v-model="form.collegeId"
                placeholder="请输入学院名称/代码"
                clearable
                filterable
                remote
                :remote-method="filterColleges"
                style="width: 100%;"
                :loading="collegeLoading"
              >
                <el-option
                  v-for="college in filteredColleges"
                  :key="college.collegeId"
                  :label="`${college.collegeName} (${college.collegeCode})`"
                  :value="college.collegeId"
                >
                  <span style="float: left">{{ college.collegeName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ college.collegeCode }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="校区" prop="campus">
              <el-select v-model="form.campus" placeholder="请选择校区">
                <el-option
                  v-for="dict in campus_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 教学楼与楼层 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="教学楼" prop="buildingId">
              <el-input v-model="form.buildingId" placeholder="请输入教学楼" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼层" prop="floor">
              <el-input v-model="form.floor" placeholder="请输入楼层" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 房间号与容量 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="具体房间号" prop="roomNumber">
              <el-input v-model="form.roomNumber" placeholder="请输入具体房间号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上课人数容量" prop="capacity">
              <el-input v-model="form.capacity" placeholder="请输入上课人数容量" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 考试容量与设备 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考试人数容量" prop="testCapacity">
              <el-input v-model="form.testCapacity" placeholder="请输入考试人数容量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备信息" prop="equipment">
              <el-input v-model="form.equipment" placeholder="请输入设备信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 设备选项 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否有黑板" prop="blackboard">
              <el-select v-model="form.blackboard" placeholder="请选择是否有黑板">
                <el-option
                  v-for="dict in y_n"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否有投影仪" prop="projector">
              <el-select v-model="form.projector" placeholder="请选择是否有投影仪">
                <el-option
                  v-for="dict in y_n"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 联系信息 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系人姓名" prop="contactName">
              <el-input v-model="form.contactName" placeholder="请输入联系人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 状态与可用性 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="审核状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择审核状态">
                <el-option
                  v-for="dict in status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排课是否可用" prop="scheduleAvailable">
              <el-select v-model="form.scheduleAvailable" placeholder="请选择排课是否可用">
                <el-option
                  v-for="dict in y_n"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 考试可用性与时间 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考试是否可用" prop="testAvailability">
              <el-select v-model="form.testAvailability" placeholder="请选择考试是否可用">
                <el-option
                  v-for="dict in y_n"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="限制时间" prop="bindingHours">
              <el-date-picker
                clearable
                v-model="form.bindingHours"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择限制时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 单双周与备注 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="单双周" prop="oddBiweekly">
              <el-input v-model="form.oddBiweekly" placeholder="请输入单双周" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注信息" prop="notes">
              <el-input v-model="form.notes" type="textarea" :rows="2" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="Classroom">
import { listClassroom, getClassroom, delClassroom, addClassroom, updateClassroom,importClassroomData } from "@/api/classroom/classroom";
import { listCollege } from "@/api/college/college";
import { getToken } from "@/utils/auth";
import { debounce } from 'lodash';
const { proxy } = getCurrentInstance();
const { campus_type, status, y_n, classroom_type } = proxy.useDict('campus_type', 'status', 'y_n', 'classroom_type');
const classroomList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const filteredColleges = ref([]);
const collegeList = ref([]);
const collegeLoading = ref(false);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    roomCode: null,
    classroomType: null,
    collegeId: null,
    campus: null,
    buildingId: null,
    classRoomName: null,
    floor: null,
    roomNumber: null,
    capacity: null,
    testCapacity: null,
    equipment: null,
    blackboard: null,
    projector: null,
    whiteboard: null,
    contactName: null,
    contactPhone: null,
    status: null,
    scheduleAvailable: null,
    testAvailability: null,
    bindingHours: null,
    oddBiweekly: null,
    notes: null
  },
  rules: {
  }
});
// 导入相关配置
const upload = ref({
  titleImport: "",
  openImport: false,
  isUploading: false,
  updateSupport: false,
  headers: {
    Authorization: "Bearer " + getToken()
  },
  url: "/classroom/classroom/importData",
});
const uploadRef = ref(null);
const importFile = ref(null);
/** 导入相关方法 */
/** 打开导入对话框 */
function handleImport() {
  console.log('准备打开导入对话框');
  upload.value.isUploading = false;
  upload.value.updateSupport = false;

  if (uploadRef.value) {
    uploadRef.value.clearFiles();
  }

  upload.value.titleImport = "导入课程信息表";
  upload.value.openImport = true;
  console.log('导入对话框状态:', upload.value.openImport);
}

/** 关闭导入对话框 */
function handleCancelImport() {
  console.log('关闭导入对话框');
  upload.value.openImport = false;
  upload.value.isUploading = false;
  importFile.value = null;

  if (uploadRef.value) {
    uploadRef.value.clearFiles();
  }
}

/** 下载导入模板 */
function importTemplate() {
  proxy.download(
      import.meta.env.VUE_APP_BASE_API + '/classroom/classroom/exportTemplate',
      {
        ...queryParams.value
      },
      `课程信息模板${new Date().getTime()}.xlsx`
  );
}

/** 文件上传进度处理 */
function handleFileUploadProgress(event, file, fileList) {
  console.log('===== 上传进度 =====');
  console.log('进度百分比:', Math.round(event.percent) + '%');
  console.log('当前文件:', file.name);
  upload.value.isUploading = true;
}

/** 文件上传成功处理 */
function handleFileSuccess(response, file, fileList) {
  console.log('===== 上传成功 =====');
  console.log('后端响应内容:', response);
  console.log('上传成功的文件:', file.name);
  upload.value.openImport = false;
  upload.value.isUploading = false;
  if (uploadRef.value) {
    uploadRef.value.clearFiles();
  }
  proxy.$alert(response.msg, "导入结果", {
    dangerouslyUseHTMLString: true
  });
  getList();
}

/** 提交上传文件（执行导入） */
function submitFileForm() {
  if (!importFile.value) {
    proxy.$message.warning('请选择要上传的文件');
    return;
  }

  console.log('使用保存的文件:', importFile.value.name);
  console.log('updateSupport参数:', upload.value.updateSupport);

  upload.value.isUploading = true;
  importClassroomData(importFile.value, upload.value.updateSupport)
      .then(response => {
        upload.value.isUploading = false;
        upload.value.openImport = false;
        importFile.value = null;
        if (uploadRef.value) {
          uploadRef.value.clearFiles();
        }
        proxy.$message.success(response.msg || '导入成功');
        getList();
      })
      .catch(error => {
        upload.value.isUploading = false;
        console.error('上传失败:', error);
        proxy.$message.error('上传失败: ' + (error.message || '未知错误'));
      });
}

/** 文件上传失败处理 */
function handleFileError(err, file, fileList) {
  console.error('===== 上传错误详情 =====');
  console.error('错误对象:', err);
  console.error('请求URL:', upload.value.url + '?updateSupport=' + upload.value.updateSupport);
  console.error('响应状态码:', err.response?.status);
  console.error('响应内容:', err.response?.data);
  console.error('错误消息:', err.message);
  upload.value.isUploading = false;
  proxy.$message.error('上传失败: ' + (err.message || '未知错误'));
}

/** 文件选择变化时保存文件引用 */
function handleFileChange(file, fileList) {
  console.log('选择的文件:', file);
  console.log('文件列表:', fileList);
  importFile.value = file.raw || file;
}
const { queryParams, form, rules } = toRefs(data);
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    roomCode: null,
    classroomType: null,
    collegeId: null,
    campus: null,
    buildingId: null,
    floor: null,
    roomNumber: null,
    capacity: null,
    testCapacity: null,
    equipment: null,
    blackboard: null,
    projector: null,
    whiteboard: null,
    contactName: null,
    contactPhone: null,
    status: null,
    scheduleAvailable: null,
    testAvailability: null,
    bindingHours: null,
    oddBiweekly: null,
    notes: null
  };
  proxy.resetForm("classroomRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加教学资源管理";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getClassroom(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改教学资源管理";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["classroomRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateClassroom(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addClassroom(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除教学资源管理编号为"' + _ids + '"的数据项？').then(function() {
    return delClassroom(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('classroom/classroom/export', {
    ...queryParams.value
  }, `classroom_${new Date().getTime()}.xlsx`)
}
// 获取学院列表并设置过滤列表
const getCollegeList = async () => {
  try {
    const res = await listCollege({ pageSize: 1000 });
    collegeList.value = res.rows;
    filteredColleges.value = res.rows;
  } catch (err) {
    console.error('获取学院列表失败:', err);
    proxy.$modal.msgError("学院数据加载失败");
  }
};
// 学院过滤方法
const filterColleges = debounce((query) => {
  collegeLoading.value = true;
  try {
    if (query) {
      const lowerQuery = query.toLowerCase();
      filteredColleges.value = collegeList.value.filter(college =>
        college.collegeName?.toLowerCase().includes(lowerQuery) ||
        college.collegeCode?.includes(query)
      );
    } else {
      filteredColleges.value = collegeList.value;
    }
  } finally {
    collegeLoading.value = false;
  }
}, 300);
// 获取学院名称
const getCollegeName = (collegeId) => {
  if (!collegeId) return '-';
  const college = collegeList.value.find(c => c.collegeId === collegeId);
  return college ? `${college.collegeName} (${college.collegeCode})` : '未知学院';
};
// 获取教室列表
const getList = () => {
  loading.value = true;
  listClassroom(queryParams.value)
    .then(response => {
      classroomList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    })
    .catch(err => {
      console.error('获取教室数据失败:', err);
      loading.value = false;
    });
};
// 初始化数据
onMounted(() => {
  getCollegeList();
  getList();
});
</script>
